//Written by Richard Lewis aka Trixmoto (c) 2007

var MyForm = MyProp = null;
var subs = new Array();
var view = 1;
var imglist = new Array('menl.png','menr.png','menfrm.png','mendoc.png','menopt.png','menpan.png','mentra.png','menbut.png','menlbl.png','menedt.png','mendrp.png','menchk.png','menrad.png','menspn.png','menbar.png','menact.png','mendel.png','mm.png','v.png','w.png','x.png','sel.png','bar.png','chk.png','chkd.png','drp.png','ie.png','rad.png','radd.png','trk.png','un.png','clip.png','file.png','mail.png','mencut.png','mencpy.png','menpst.png','menudo.png','menrdo.png','base.gif','empty.gif','folder.gif','folderopen.gif','join.gif','joinbottom.gif','line.gif','minus.gif','minusbottom.gif','page.gif','plus.gif','plusbottom.gif','trepan.gif','tretra.gif','trebut.gif','trelbl.gif','treedt.gif','tredrp.gif','trechk.gif','trerad.gif','trespn.gif','trebar.gif','treact.gif','resize.png');
var javlist = new Array('show.js','drag.js','ui.js','new.js','draw.js','prop.js','evnt.js','tools.js','tree.js','dtree.js');
var sdone = idone = false;
var debugCounter = 0;

function tell(msg) {
  // return;
  debugCounter++;
  var t = document.createTextNode("\n"+debugCounter+": "+msg);
  var br = document.createElement("br");
  var debug = document.getElementById("debug");
  debug.appendChild(t);
  debug.appendChild(br);
}

function init() {
  document.getElementById('tabs').innerHTML = addtabs();
  document.getElementById('prop').innerHTML = welcome();  
  var ip = new ImagePreloader(imglist);
  preloadscripts(0);    
  return true;
}

function addtabs() {
  var tab = "<div id='header'><ul>";
  var id1 = id2 = id3 = id4 = "";
  switch (view) {
    case (1):
      id1 = " id='current'";
      break;
    case (2):
      id2 = " id='current'";
      break;
    case (3):
      id3 = " id='current'";
      break;
    case (4):
      id4 = " id='current'";
      break;
  }
  tab += "<li"+id1+"><a href='javascript://Properties' onclick='showprop()'>Properties</a></li>";
  tab += "<li"+id2+"><a href='javascript://Events' onclick='showevnt()'>Events</a></li>";
  tab += "<li"+id3+"><a href='javascript://Objects' onclick='showtree()'>Objects</a></li>";
  tab += "<li"+id4+"><a href='javascript://Source' onclick='showcode()'>Source</a></li>";
  tab += "</ul></div>";
  return tab;
}

function welcome() {
  var mes = "<br>Welcome!<br><br>";
  mes += "Please start by adding a new form using one of the toolbar buttons. Continue adding objects and updating their properties - you can drag objects to the desired position.<br><br>";
  mes += "Click on the Events tab to add events to the selected object and click on the Objects tab to see a tree view of all the existing objects.<br><br>";
  mes += "When you are ready, click the Source tab to see the source code that will create this form. From here you can easily copy, download or email this code.<br><br>";
  mes += "Regards, Richard Lewis (aka Trixmoto)<br><br>";
  return mes;
}

function checkdone() {
  if(sdone && idone) {
    document.getElementById('tool').innerHTML = toolbar();  
    document.getElementById('form').innerHTML = " ";
  }
}

function preloadscripts(i) {
  var s = document.createElement('script');
  s.type = "text/javascript";
  s.src = javlist[i];
  var h = document.getElementsByTagName("head")[0];
  h.appendChild(s);
  i++;  
  document.getElementById("sprog").style.width = Math.round(i*400/javlist.length);
  if(i<javlist.length) {
    setTimeout('preloadscripts('+i+')',500);
  }
  else {
    sdone = true;
    checkdone();
  }
  return true;
}

function ImagePreloader(images) {
  this.nLoaded = 0;
  this.nFailed = 0;
  this.nProcessed = 0;
  this.aImages = new Array();
  this.nImages = images.length;
  for(var i=0;i<images.length;i++) {
    this.preload(images[i]);
  }
}

ImagePreloader.prototype.preload = function(image) {
  var oImage = new Image;
  this.aImages.push(oImage);
  oImage.onload = ImagePreloader.prototype.onload;
  oImage.onerror = ImagePreloader.prototype.onerror;
  oImage.onabort = ImagePreloader.prototype.onabort;
  oImage.oImagePreloader = this;
  oImage.bLoaded = false;
  oImage.bError = false;
  oImage.bAbort = false;
  oImage.src = "imgs/"+image;
}

ImagePreloader.prototype.onComplete = function() {
  this.nProcessed++;
  document.getElementById("iprog").style.width = Math.round(this.nProcessed*400/this.nImages);
  if(this.nProcessed==this.nImages) {
    idone = true;
    checkdone();
  }
}

ImagePreloader.prototype.onload = function() {
  this.bLoaded = true;
  this.oImagePreloader.nLoaded++;
  this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function() {
  tell("Error:"+this.src);
  this.bError = true;
  this.oImagePreloader.nFailed++;
  document.getElementById("eprog").style.width = Math.round(this.oImagePreloader.nFailed*400/imglist.length);
  this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function() {
  tell("Abort:"+this.src);
  this.bAbort = true;
  this.oImagePreloader.nFailed++;
  var w = document.getElementById("eprog").style.width.replace(/px/,"")*1;
  document.getElementById("eprog").style.width = w+Math.round(400/imglist.length);
  this.oImagePreloader.onComplete();
}

function toolbar() {
  //forms
  var bar = "<img src='imgs/menl.png' border=0>";
  bar += "<a href='javascript://New Form' onclick=\"newform('frm')\">";
  bar += "<img src='imgs/menfrm.png' border=0></a>";
  bar += "<a href='javascript://New DockablePanel' onclick=\"newform('doc')\">";
  bar += "<img src='imgs/mendoc.png' border=0></a>";
  bar += "<a href='javascript://New OptionSheet' onclick=\"newform('opt')\">";
  bar += "<img src='imgs/menopt.png' border=0></a>";  
  bar += "<img src='imgs/menr.png' border=0>";
  
  if(MyForm!=null) {      
    //objects
    bar += "<img src='imgs/menl.png' border=0>";
    bar += "<a href='javascript://Add Panel' onclick=\"addobject('pan')\">";
    bar += "<img src='imgs/menpan.png' border=0></a>";
    bar += "<a href='javascript://Add TranspPanel' onclick=\"addobject('tra')\">";
    bar += "<img src='imgs/mentra.png' border=0></a>";  
    bar += "<a href='javascript://Add Button' onclick=\"addobject('but')\">";
    bar += "<img src='imgs/menbut.png' border=0></a>";
    bar += "<a href='javascript://Add Label' onclick=\"addobject('lbl')\">";
    bar += "<img src='imgs/menlbl.png' border=0></a>";  
    bar += "<a href='javascript://Add Edit' onclick=\"addobject('edt')\">";
    bar += "<img src='imgs/menedt.png' border=0></a>";  
    bar += "<a href='javascript://Add DropDown' onclick=\"addobject('drp')\">";
    bar += "<img src='imgs/mendrp.png' border=0></a>";
    bar += "<a href='javascript://Add CheckBox' onclick=\"addobject('chk')\">";
    bar += "<img src='imgs/menchk.png' border=0></a>";     
    bar += "<a href='javascript://Add RadioButton' onclick=\"addobject('rad')\">";
    bar += "<img src='imgs/menrad.png' border=0></a>"; 
    bar += "<a href='javascript://Add SpinEdit' onclick=\"addobject('spn')\">";
    bar += "<img src='imgs/menspn.png' border=0></a>";  
    bar += "<a href='javascript://Add TrackBar' onclick=\"addobject('bar')\">";
    bar += "<img src='imgs/menbar.png' border=0></a>";
    bar += "<a href='javascript://Add ActiveX' onclick=\"addobject('act')\">";
    bar += "<img src='imgs/menact.png' border=0></a>";  
    bar += "<img src='imgs/menr.png' border=0>";
    
    //tools
    var menr = false;
    if(MyProp.prnt!=null) {  
      bar += "<img src='imgs/menl.png' border=0>";
      bar += "<a href='javascript://Delete selected' onclick=\"delobject()\">";
      bar += "<img src='imgs/mendel.png' border=0></a>";
      bar += "<a href='javascript://Cut selected' onclick=\"cutobject()\">";
      bar += "<img src='imgs/mencut.png' border=0></a>";
      if(MyProp.kids==null) {
        bar += "<a href='javascript://Copy selected' onclick=\"copyobject()\">";
        bar += "<img src='imgs/mencpy.png' border=0></a>";
      }
      menr = true; 
    }
    if(MyProp.kids!=null && clipboard!=null) {
      if(!menr) {
        bar += "<img src='imgs/menl.png' border=0>";
      }
      bar += "<a href='javascript://Paste selected' onclick=\"pasteobject()\">";
      bar += "<img src='imgs/menpst.png' border=0></a>";
      menr = true;
    }
    if(menr) {
      bar += "<img src='imgs/menr.png' border=0>";    
    }
    
    //undo
    if(memory.length>1) {
      bar += "<img src='imgs/menl.png' border=0>";
      if(mempos>0) {
        bar += "<a href='javascript://Undo selected' onclick=\"undo()\">";
        bar += "<img src='imgs/menudo.png' border=0></a>";
      }
      if(mempos<(memory.length-1)) {
        bar += "<a href='javascript://Redo selected' onclick=\"redo()\">";
        bar += "<img src='imgs/menrdo.png' border=0></a>";
      }
      bar += "<img src='imgs/menr.png' border=0>";    
    }
  }
  return bar;
}